package com.zxj.hadoop.demo.mapreduce.outputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @Author 朱小杰
 * 时间 2017-08-26 .21:15
 * 说明 ...
 */
public class Driver {

    static class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context)
                throws IOException, InterruptedException {
            //这是mapreduce读取到的一行字符串
            String line = value.toString();
            String[] words = line.split(" ");
            for (String word : words) {
                //将单词输出为key，次数输出为value，这行数据会输到reduce中
                context.write(new Text(word), new LongWritable(1));
            }
        }
    }


    static class WordCountReduce extends Reducer<Text, LongWritable, Text, LongWritable> {

        @Override
        protected void reduce(Text key, Iterable<LongWritable> values,
                              Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
            //直接设定数值
            context.getCounter("","").setValue(10);
            long count = 0;
            for (LongWritable num : values) {
                count += num.get();
            }
            context.write(key, new LongWritable(count));
        }
    }

    public static void main(String[] args) throws IOException {
        Configuration conf = new Configuration();
        //这个默认值就是local，其实可以不写
        conf.set("mapreduce.framework.name", "local");
        //本地模式运行mr程序时，输入输出可以在本地，也可以在hdfs中，具体需要看如下的两行参数
        //这个默认值 就是本地，其实可以不配
        //conf.set("fs.defaultFS","file:///");
        //conf.set("fs.defaultFS","hdfs://server1:9000/");



        Job job = Job.getInstance(conf);

        //使得hadoop可以根据类包，找到jar包在哪里
        job.setJarByClass(Driver.class);

        //设置自定义的OutputFormat
        job.setOutputFormatClass(MyOutputFormat.class);

        //指定Mapper的类
        job.setMapperClass(WordCountMapper.class);
        //指定reduce的类
        job.setReducerClass(WordCountReduce.class);

        //设置Mapper输出的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        //设置最终输出的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        //指定输入文件的位置，这里为了灵活，接收外部参数
        FileInputFormat.setInputPaths(job, new Path("D:\\mr\\wordcount\\input"));
        //指定输入文件的位置，这里接收启动参数
        FileOutputFormat.setOutputPath(job, new Path("D:\\mr\\wordcount\\out1"));

        //将job中的参数，提交到yarn中运行
        //job.submit();
        try {
            job.waitForCompletion(true);
            //这里的为true,会打印执行结果
        } catch (ClassNotFoundException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
